package com.dobby.logger.business.controller;

import com.dobby.logger.business.entity.BizLog;
import com.dobby.logger.business.service.BizLogExcelView;
import com.dobby.logger.business.service.BizLogService;
import com.dobby.logger.common.OpenPage;
import com.dobby.logger.constants.Message;
import com.dobby.logger.utils.CommonUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * Created by liguoqing on 2016/3/7.
 */
@Controller
@RequestMapping(value = "/bizLog")
public class BizLogController {


    @Autowired
    private BizLogService bizLogService;



    @RequestMapping(value = "/search")
    public String list(@RequestParam(value = "companyId", required = false, defaultValue = "") String companyId,
                       @RequestParam(value = "operatorType", required = false, defaultValue = "") String operatorType,
                       @RequestParam(value = "moduleId", required = false, defaultValue = "") String moduleId,
                       @RequestParam(value = "applicationId", required = false, defaultValue = "") String applicationId,
                       @RequestParam(value = "visible", required = false, defaultValue = "") String visible,
                       @RequestParam(value = "datetimeStart", required = false, defaultValue = "") String datetimeStart,
                       @RequestParam(value = "datetimeEnd", required = false, defaultValue = "") String datetimeEnd,
                       @RequestParam(value = "content", required = false, defaultValue = "") String content,
                       @RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageNo,
                       @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                       @RequestParam(value = "collapse", required = false, defaultValue = "hide") String collapse,
                       ModelMap modelMap) {
        Map<String,String> conditionMap = Maps.newHashMap();
        conditionMap.put("operatorType",operatorType);
        conditionMap.put("companyId", companyId);
        conditionMap.put("moduleId",moduleId.trim());
        conditionMap.put("applicationId",applicationId.trim());
        conditionMap.put("visible",visible);
        conditionMap.put("content",content);
        conditionMap.put("datetimeStart",datetimeStart);
        conditionMap.put("datetimeEnd", CommonUtils.DateAdd(datetimeEnd, 1, Calendar.DATE)); // 结束日期向后加一天

        OpenPage<BizLog> openPage = new OpenPage<BizLog>();
        openPage.setPageNo(pageNo);
        openPage.setPageSize(pageSize);
        openPage = bizLogService.get(conditionMap, openPage);

        modelMap.put("bizLogs", openPage.getRows());
        modelMap.put("pageFirst", openPage.getFirst());
        modelMap.put("openPage", openPage);
        modelMap.put("companyId", companyId);
        modelMap.put("operatorType", operatorType);
        modelMap.put("moduleId", moduleId);
        modelMap.put("applicationId", applicationId);
        modelMap.put("visible", visible);
        modelMap.put("content", content);
        modelMap.put("datetimeStart", datetimeStart);
        modelMap.put("datetimeEnd", datetimeEnd);
        modelMap.put("collapse", collapse);

        return "business/list";
    }

    @RequestMapping(value = "edit")
    public String edit(@RequestParam(value = "bizLogId",required = true,defaultValue = "") String bizLogId,
                       ModelMap modelMap) {
        BizLog bizLog = bizLogService.getById(bizLogId);
        modelMap.put("bizLog",bizLog);
        return "business/form";
    }


    @RequestMapping(value = "saveOrUpdate")
    public String saveOrUpdate(@RequestParam(value = "bizLogId", required = true, defaultValue = "") String bizLogId,
                               @RequestParam(value = "companyId", required = true, defaultValue = "") String companyId,
                               @RequestParam(value = "applicationId", required = true, defaultValue = "") String applicationId,
                               @RequestParam(value = "moduleId", required = true, defaultValue = "") String moduleId,
                               @RequestParam(value = "content", required = true, defaultValue = "") String content,
                               @RequestParam(value = "operatorType", required = true, defaultValue = "") Long operatorType,
                               @RequestParam(value = "visible", required = true, defaultValue = "false") boolean visible
                               ) {
        if(StringUtils.isEmpty(bizLogId)) {
            BizLog bizLog  = new BizLog();
            bizLog.setId(CommonUtils.getUUID());
            bizLog.setCompanyId(companyId);
            bizLog.setApplicationId(applicationId);
            bizLog.setModuleId(moduleId);
            bizLog.setContent(content);
            bizLog.setOperatorId("lgq");
            bizLog.setOperatorType(operatorType);
            bizLog.setOperateDate(CommonUtils.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
            bizLog.setVisible(visible);
            bizLog.setIpAddress(CommonUtils.getHostIp());
            bizLogService.save(bizLog);
        } else {
            BizLog bizLog = bizLogService.getById(bizLogId);
            bizLog.setCompanyId(companyId);
            bizLog.setApplicationId(applicationId);
            bizLog.setModuleId(moduleId);
            bizLog.setContent(content);
            bizLog.setOperatorId("lgq");
            bizLog.setOperatorType(operatorType);
            bizLog.setOperateDate(CommonUtils.DateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
            bizLog.setVisible(visible);
            bizLog.setIpAddress(CommonUtils.getHostIp());
            bizLogService.update(bizLog);
        }
        return "redirect:/bizLog/search";
    }


    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(@RequestParam(value = "bizLogId", required = false, defaultValue = "") String bizLogId,
                         @RequestParam(value = "bizLogIds", required = false, defaultValue = "") String bizLogIds) {
        String msg = "";
        if(StringUtils.isEmpty(bizLogIds)) {
            msg = bizLogService.delete(bizLogId);
        } else {
            List<String> ids = Lists.newArrayList(StringUtils.split(bizLogIds,","));
            msg = bizLogService.delete(ids);
        }
        return msg;
    }


    /**
     * 检查代码是否唯一
     */
    @RequestMapping(value = "/checkDuplicate")
    public @ResponseBody
    String checkDuplicate(@RequestParam(value = "code", required = false, defaultValue = "") String code) {
        boolean isDuplicate = bizLogService.checkDuplicate(code);
        if(isDuplicate) {
            return Message.ERROR;
        } else {
            return Message.SUCCESS;
        }
    }


    /**
     * 导出excel，可批量
     */
    @RequestMapping(value = "/export")
    public ModelAndView export(@RequestParam(value = "bizLogIds", required = false, defaultValue = "") String bizLogIds,
                               @RequestParam(value = "companyId", required = false, defaultValue = "") String companyId,
                               @RequestParam(value = "operatorType", required = false, defaultValue = "") String operatorType,
                               @RequestParam(value = "moduleId", required = false, defaultValue = "") String moduleId,
                               @RequestParam(value = "applicationId", required = false, defaultValue = "") String applicationId,
                               @RequestParam(value = "visible", required = false, defaultValue = "") String visible,
                               @RequestParam(value = "datetimeStart", required = false, defaultValue = "") String datetimeStart,
                               @RequestParam(value = "datetimeEnd", required = false, defaultValue = "") String datetimeEnd,
                               @RequestParam(value = "content", required = false, defaultValue = "") String content,
                               @RequestParam(value = "pageNo", required = false, defaultValue = "1") int pageNo,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                               ModelMap modelMap) {
        List<BizLog> bizLogList = Lists.newArrayList();
        if(StringUtils.isEmpty(bizLogIds)) {
            Map<String,String> conditionMap = Maps.newHashMap();
            conditionMap.put("operatorType",operatorType);
            conditionMap.put("moduleId",moduleId.trim());
            conditionMap.put("applicationId",applicationId.trim());
            conditionMap.put("visible",visible);
            conditionMap.put("content",content);
            conditionMap.put("datetimeStart",datetimeStart);
            conditionMap.put("datetimeEnd", CommonUtils.DateAdd(datetimeEnd, 1, Calendar.DATE)); // 结束日期向后加一天

            OpenPage<BizLog> openPage = new OpenPage<BizLog>();
            openPage.setPageNo(pageNo);
            openPage.setPageSize(Integer.MAX_VALUE);
            openPage = bizLogService.get(conditionMap, openPage);
            bizLogList = openPage.getRows();
        } else {
            String[] idsArray = StringUtils.split(bizLogIds, ",");
            bizLogList = bizLogService.getByIds(Lists.newArrayList(idsArray));
        }

        // excel导出数据
        BizLogExcelView bizLogExcelView = new BizLogExcelView();
        String[] keys = {"companyId","applicationId","moduleId","operatorId","operatorType","content","visible","ipAddress","operateDate"};
        String[] titles = {"企业","应用","模块","操作人","日志类型","内容","是否可见","IP地址","操作日期"};
        String fileName = "日志信息";
        modelMap.put("keys", keys);
        modelMap.put("titles", titles);
        modelMap.put("fileName", fileName);
        modelMap.put("datas", bizLogList);
        return new ModelAndView(bizLogExcelView, modelMap);

    }

    @RequestMapping(value = "/batchOpen")
    public String batchOpen(@RequestParam(value = "bizLogIds", required = false, defaultValue = "") String bizLogIds,
                            @RequestParam(value = "visible", required = false, defaultValue = "false") boolean visible) {
        String[] idsArray = StringUtils.split(bizLogIds, ",");
        List<BizLog> bizLogList = bizLogService.getByIds(Lists.newArrayList(idsArray));
        for(BizLog bizLog : bizLogList) {
            bizLog.setVisible(visible);
            bizLogService.update(bizLog);
        }
        return "redirect:/bizLog/search";
    }



}
